package com.smforj.ssm.modules.eslib.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smforj.ssm.frame.core.dao.BaseDao;
import com.smforj.ssm.frame.core.service.impl.BaseServiceImpl;
import com.smforj.ssm.modules.eslib.dao.CirculationDao;
import com.smforj.ssm.modules.eslib.dao.ReaderDao;
import com.smforj.ssm.modules.eslib.domain.Circulation;
import com.smforj.ssm.modules.eslib.domain.Reader;
import com.smforj.ssm.modules.eslib.domain.ReaderVo;
import com.smforj.ssm.modules.eslib.service.ReaderService;
import com.smforj.ssm.modules.lib.domain.PatronResponse;
import com.smforj.ssm.util.DateFormat;

@Service 
public class ReaderServiceImpl extends BaseServiceImpl<Reader> implements
		ReaderService {
	/**
	 * 日志
	 */
	private static Logger log = Logger.getLogger("ReaderServiceImpl");
	
	@Autowired 
	private ReaderDao readerDao;
	@Autowired
	private CirculationDao circulationDao;

	@Override
	protected BaseDao<Reader> getBaseDao() { 
		return readerDao;
	}

	@Override
	public PatronResponse getReader(String userId) { 
		
		Reader query = new Reader();
		query.setJszh(userId);
		PatronResponse patron = new PatronResponse(); 
		try{
			ReaderVo rv = readerDao.selectOne(query);  
			patron.setStatus(1);    
			patron.setReaderName(rv.getDzmc());
			patron.setHoldItemsCount(rv.getYjs().toString());
			patron.setFeeMax(Double.parseDouble(rv.getYqfk())); 
			 
			List<Circulation> list = circulationDao.getCirculations(rv.getJszh());
			int yqCount = 0;
			if(list != null && list.size() > 0)
			{
				String[] holdBooks = new String[list.size()];
				for(int i=0;i<list.size();i++) 
				{
					holdBooks[i] = list.get(i).getSm()+"              "+list.get(i).getYhrq();  
					//超期
					if(DateFormat.stringToDate(list.get(i).getYhrq()).getTime() > new Date().getTime())
					{
						yqCount++;
					}
				}
				patron.setHoldBooks(holdBooks);
			} 
			patron.setFine(Double.parseDouble(rv.getYqfk()) * yqCount); 
		}catch(Exception ex)
		{
			log.error("查询读者信息异常："+ex.getCause().getMessage());
			patron.setStatus(0); 
			patron.setMessage("查询读者信息异常："+ex.getMessage());
		} 
		return patron;
	} 
}
